package com.pzhu.record.service.impl;

import com.pzhu.record.constants.Constant;
import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Page;
import com.pzhu.record.service.BasicinformationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 学生信息业务实验
 *
 * @author Zhou YuSen
 * @date 2020/4/2 18:11
 */
@Service("basicinformationService")
public class BasicinformationServiceImpl extends BaseServiceImpl implements BasicinformationService {
    @Override
    public Basicinformation selectByIdOrpass(String basicinformationId, String basicinformationPassword) {
        return basicinformationMapper.selectByIdOrpass(basicinformationId, basicinformationPassword);
    }

    @Override
    public int insertBatch(List<Basicinformation> basicinformationList) {
        //循环遍历读取Excel的对象集合
        for (Basicinformation basicinformation : basicinformationList) {
            //获取年级专业班级
            String basicinformationClass = basicinformation.getBasicinformationClass();
            String stuId = null;
            //截取年级专业班级的前四位，即年级，如2016
            String grade = basicinformationClass.substring(0, 4);
            //获取学院的编号
            String instituteNume = basicinformation.getInstituteNume();
            //调用方法，通过年级与学院查询出最大的学号
            String maxId = selectMaxId(instituteNume, "%" + grade + "%");
            int anInt = 0;
            int len = 0;
            //判断查询是否为空
            if (maxId != null) {
                //截取最大学号的最后四位，并转换为数字后 + 1，产生新学号
                anInt = Integer.parseInt(maxId.substring(maxId.length() - 4)) + 1;
                len = String.valueOf(anInt).length();
            } else {
                //如果通过年级与学院为查询出值，则设置为第一个学生，即：学号后四位为0001
                anInt = 1;
                len = 1;
            }
            StringBuilder m = new StringBuilder();
            if (len < 4) {
                for (int i = len; i < 4; i++) {
                    //为查询出的学号后四位转为数值后不足四位的补零，满足最后为四位，保证学号长度一致
                    m.append("0");
                }
            }
            //在数据库中设置学院的编号，以此判断添加的学生信息满足哪一个学院
            if ("1".equals(instituteNume)) {
                /*数学与计算机学院*/
                //将年级 + 学院对应编号 + 补全0 + 获得的学号 进行拼接，最终获得新学生的学号
                stuId = grade + Constant.INTERNATE_NUMBER_ID + m + anInt;
            } else if ("2".equals(instituteNume)) {
                /*智能制造学院*/
                stuId = grade + Constant.MAKE_NUMBER_ID + m + anInt;
            } else if ("3".equals(instituteNume)) {
                /*英语学院*/
                stuId = grade + Constant.ENGLIDH_NUMBER_ID + m + anInt;
            } else if ("4".equals(instituteNume)) {
                /*医学院*/
                stuId = grade + Constant.MEDICAL_NUMBER_ID + m + anInt;
            }
            //将学号存到当前学生对象
            basicinformation.setBasicinformationId(stuId);
        }
        return basicinformationMapper.insertBatch(basicinformationList);
    }

    @Override
    public Page<Basicinformation> selectPage(int pageIndex, Basicinformation basicinformation) {
        Page<Basicinformation> page = new Page<>();
        page.setParamEntity(basicinformation);
        Integer pageNumber = selectPageCount(page);
        List<Basicinformation> list = new ArrayList<>();
        if (pageIndex < 1) {
            pageIndex = 1;
        }
        page.setPageIndex(pageIndex);
        page.setPageNumber(pageNumber);
        if (page.getPageNumber() % page.getPageSize() == 0) {
            page.setPageCount(page.getPageNumber() / page.getPageSize());
        } else {
            page.setPageCount(page.getPageNumber() / page.getPageSize() + 1);
        }
        list = basicinformationMapper.selectPageList(page);
        page.setList(list);
        return page;
    }

    @Override
    public Integer selectPageCount(Page<Basicinformation> basicinformationPage) {
        return basicinformationMapper.selectPageCount(basicinformationPage);
    }

    @Override
    public List<Basicinformation> selectExport(Map<String, String> keywords) {
        return basicinformationMapper.selectExport(keywords);
    }

    @Override
    public String selectMaxId(String InstituteNume, String grade) {
        return basicinformationMapper.selectMaxId(InstituteNume, grade);
    }

    @Override
    public int insert(Basicinformation basicinformation) {
        return 0;
    }

    @Override
    public int update(Basicinformation basicinformation) {
        return basicinformationMapper.updateByPrimaryKeySelective(basicinformation);
    }

    @Override
    public int delete(String id) {
        return 0;
    }

    @Override
    public int deleteAll(String[] pks) {
        return 0;
    }

    @Override
    public Basicinformation selectById(String id) {
        return basicinformationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Basicinformation> selectAll() {
        return null;
    }

    @Override
    public int insertSelective(Basicinformation basicinformation) {

        basicinformation.setBasicinformationRole("学生");
        int parseInt = Integer.parseInt(basicinformation.getBasicinformationSex());
        if (1 == parseInt) {
            basicinformation.setBasicinformationSex("女");
        } else if (0 == parseInt) {
            basicinformation.setBasicinformationSex("男");
        }
        String basicinformationClass = basicinformation.getBasicinformationClass();
        String stuId = null;
        String grade = basicinformationClass.substring(0, 4);
        String instituteNume = basicinformation.getInstituteNume();
        String maxId = selectMaxId(instituteNume, "%" + grade + "%");
        int anInt = 0;
        int len = 0;
        if (maxId != null) {
            String subMax = maxId.substring(maxId.length() - 4);

            anInt = Integer.parseInt(subMax) + 1;
            len = String.valueOf(anInt).length();
        } else {
            anInt = 1;
            len = 1;
        }
        StringBuilder m = new StringBuilder();
        if (len < 4) {
            for (int i = len; i < 4; i++) {
                m.append("0");
            }
        }
        if ("1".equals(instituteNume)) {
            /*数学与计算机学院*/
            stuId = grade + Constant.INTERNATE_NUMBER_ID + m + anInt;
        } else if ("2".equals(instituteNume)) {
            /*智能制造学院*/
            stuId = grade + Constant.MAKE_NUMBER_ID + m + anInt;
        } else if ("3".equals(instituteNume)) {
            /*英语学院*/
            stuId = grade + Constant.ENGLIDH_NUMBER_ID + m + anInt;
        } else if ("4".equals(instituteNume)) {
            /*医学院*/
            stuId = grade + Constant.MEDICAL_NUMBER_ID + m + anInt;
        }
        basicinformation.setBasicinformationId(stuId);
        return basicinformationMapper.insertSelective(basicinformation);
    }

}
